package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.TopicBatchDeleteVO;
import com.cskaoyan.bean.vo.back.TopicCreateVO;
import com.cskaoyan.bean.vo.back.TopicReadGoodsVO;
import com.cskaoyan.bean.vo.back.TopicReadVO;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 12-江涛
 * @since 2022/10/13  9:04
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public CommonData<MarketTopic> topicList(BasePageInfo basePageInfo, String title, String subtitle) {


        MarketTopicExample marketTopicExample = new MarketTopicExample();
        //1.查询
        marketTopicExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        //查表
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        //2.查询
        if (title != null) {
            criteria.andTitleLike("%"+title+"%");
        }
        if (subtitle != null) {
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);


        CommonData<MarketTopic> commonData = new CommonData<>();
        commonData.setList(marketTopics);
        commonData.setTotal((int) marketTopicPageInfo.getTotal());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(marketTopicPageInfo.getPageNum());
        commonData.setPages(marketTopicPageInfo.getPages());

        return commonData;
    }

    @Override
    public TopicCreateVO topicCreate(MarketTopic marketTopic) {

        Date date = new Date();
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);
        marketTopic.setDeleted(false);

        int count = marketTopicMapper.insertSelective(marketTopic);

        if (count == 1) {

            TopicCreateVO topicCreateVO = new TopicCreateVO();
            topicCreateVO.setAddTime(date);
            topicCreateVO.setContent(marketTopic.getContent());
            topicCreateVO.setGoods(marketTopic.getGoods());
            topicCreateVO.setId(marketTopic.getId());
            topicCreateVO.setPrice(marketTopic.getPrice());
            topicCreateVO.setSubtitle(marketTopic.getSubtitle());
            topicCreateVO.setTitle(marketTopic.getTitle());
            topicCreateVO.setUpdateTime(date);

            return topicCreateVO;
        }

        return null;
    }

    @Override
    public TopicReadVO topicRead(Integer id) {

        //查topic表
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);

        String[] goods = marketTopic.getGoods();
        TopicReadVO topicReadVO = new TopicReadVO();
        List<TopicReadGoodsVO> goodsList = new ArrayList<>();
        for (String good : goods) {
            int goodsId = Integer.parseInt(good);
            //根据id查goods表
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsId);

            TopicReadGoodsVO topicReadGoodsVO = new TopicReadGoodsVO();

            topicReadGoodsVO.setId(marketGoods.getId());
            topicReadGoodsVO.setName(marketGoods.getName());
            topicReadGoodsVO.setBrief(marketGoods.getBrief());
            topicReadGoodsVO.setPicUrl(marketGoods.getPicUrl());
            topicReadGoodsVO.setShareUrl(marketGoods.getShareUrl());
            topicReadGoodsVO.setIsNew(marketGoods.getIsNew());
            topicReadGoodsVO.setIsHot(marketGoods.getIsHot());
            topicReadGoodsVO.setRetailPrice(marketGoods.getRetailPrice());

            goodsList.add(topicReadGoodsVO);
        }

        topicReadVO.setTopic(marketTopic);
        topicReadVO.setGoodsList(goodsList);

        return topicReadVO;
    }

    @Override
    public int topicUpdate(MarketTopic marketTopic) {

        int count = marketTopicMapper.updateByPrimaryKeySelective(marketTopic);

        return count;
    }

    @Override
    public int topicDelete(MarketTopic marketTopic) {


        marketTopic.setDeleted(true);
        int count = marketTopicMapper.updateByPrimaryKeySelective(marketTopic);

        return count;

    }

    @Override
    public int batchDelete(TopicBatchDeleteVO topicBatchDeleteVO) {

        Integer[] ids = topicBatchDeleteVO.getIds();

        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setDeleted(true);
        MarketTopicExample marketTopicExample = new MarketTopicExample();

        int count = 0;
        for (Integer id : ids) {

            marketTopicExample.createCriteria().andIdEqualTo(id);

            count += marketTopicMapper.updateByExampleSelective(marketTopic, marketTopicExample);
        }

        if (count == ids.length) {
            return 1;
        }
        return 0;
    }
}
